<?php

//extend db model voor db functions
class email extends dbModel {

	//set default table
	protected $sSource = "connect_student_class";
	
	//function for mailing all students from a given class
	public function emailClass($class_code, $module_code) {

		//write the query
		$sSql = "SELECT 
					csc.student_code, r.rating_id
				FROM 
					connect_student_class csc, ratings r
				WHERE 
					csc.class_code = '$class_code'
				AND r.student_code = csc.student_code
				AND r.module_code = '$module_code'
				";
		
		$this->trashcan();
		
		//fake the dbModel function
		$this->run_query($sSql);
		$this->oBench->start_mark();
		$this->iTotal = mysql_num_rows($this->rResult);
		$this->oBench->add_mark('counted rows');
		$this->dump();
		
		$iMails = 0;
		
		//loop trough all students and send the mail
		foreach ($this as $row) {
			
			// echo $row['student_code'];
			 
			//sample to create a PDF with a rating, needs a student and a module
			$pdf = new PDFWriter();
			$createdPDF = $pdf->getPDF($row['student_code'],$module_code);
			
			//write PDF in temp file
			$pdfcode = $createdPDF;
			$fp=fopen('temp.pdf','wb');
			fwrite($fp,$pdfcode);
			fclose($fp);
			
			//basic email settings
			// Path to the file 
			$fileatt = "temp.pdf"; 
			// File Type
			$fileatt_type = "application/octet-stream";  
			// Filename that will be used for the file as the attachment 
			$fileatt_name = $row['student_code']."_beoordeling_".$module_code.".pdf"; 
			// Who the email is from 
			$email_from = "noreply@rimmertapp.nl"; 
		    // The Subject of the email 
			$email_subject = "Beoordeling voor".$module_code;
			// Message that the email has in it 
			$email_message = "Beste student, je beoordeling voor het vak".$module_code."zit bijgevoegd."; 
			
			// Who the email is to, SPAM ALERT in this loop
			//$email_to = $row['student_code']."@student.hro.nl";
			
			//send al this spam to Mathijs (yay!)
			$email_to = "mathijs.meijer@gmail.com";
			
			$headers = "From: ".$email_from;
			
			
			//needed to encrypt file
			$semi_rand = md5(time()); 
			$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
			
			$headers .= "\nMIME-Version: 1.0\n" . 
			"Content-Type: multipart/mixed;\n" . 
			" boundary=\"{$mime_boundary}\""; 
			
			$email_message .= "This is a multi-part message in MIME format.\n\n" . 
			"--{$mime_boundary}\n" . 
			"Content-Type:text/html; charset=\"iso-8859-1\"\n" . 
			"Content-Transfer-Encoding: 7bit\n\n" . 
			$email_message . "\n\n"; 
			

			//the attachment
			$fileatt = "temp.pdf"; // Path to the file 
			$fileatt_type = "application/octet-stream"; // File Type 
			$fileatt_name = $row['student_code']."_beoordeling_".$module_code.".pdf"; // Filename that will be used for the file as the attachment 
			
			$file = fopen($fileatt,'rb'); 
			$data = fread($file,filesize($fileatt)); 
			fclose($file); 
			
			
			$data = chunk_split(base64_encode($data)); 
			
			$email_message .= "--{$mime_boundary}\n" . 
			"Content-Type: {$fileatt_type};\n" . 
			" name=\"{$fileatt_name}\"\n" . 
			//"Content-Disposition: attachment;\n" . 
			//" filename=\"{$fileatt_name}\"\n" . 
			"Content-Transfer-Encoding: base64\n\n" . 
			$data . "\n\n" . 
			"--{$mime_boundary}\n"; 
			unset($data);
			unset($file);
			unset($fileatt);
			unset($fileatt_type);
			unset($fileatt_name);
			
			$ok = @mail($email_to, $email_subject, $email_message, $headers); 
			
			//if ($ok) {
				$iMails++;
			//}
			
			/* if($ok) { 
			echo "<font face=verdana size=2>The file was successfully sent!</font>"; 
			} else { 
			die("Sorry but the email could not be sent. Please go back and try again!"); 
			} */
			
			//send the PDF to hell
			unlink('temp.pdf');
			
		}
		echo json_encode(Array('mails' => $iMails));
	}
}

?>